﻿using Aker.DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace Aker
{
    public partial class AddProduct : XPopupForm
    {
        bool includeSentez = false;
        public AddProduct(bool includeSentez = false)
        {
            InitializeComponent();
            this.includeSentez = includeSentez;
        }

        public delegate void ProductSelectedHandeler(int productId, string article);
        public event ProductSelectedHandeler ProductSelected;


        public int ProductId
        {
            get
            {
                return gleProduct.EditValue.ToIntNN();
            }
        }

        public string Artikel
        {
            get
            {
                return gleProduct.Text;

            }
        }

        private void AddProduct_Load(object sender, EventArgs e)
        {
            if (includeSentez)
            {
                DataTable dt = Data.DM.ExecuteDataTableByQuery("select * from vStokKart_Tum");
                gleProduct.Properties.Assign(Utility.GetAutoCompGLE(dt, "Article", "Article"));
            }
            else
                gleProduct.Properties.Assign(Utility.GetAutoCompGLE(Data.DB.Product, "Article", "ProductId"));

            //this.Text = Language.Fair.AddProduct;
            //lblPassword.Text = Language.Product.Article;
            //btnAdd.Text = Language.AddProduct.Add;
        }

        private void AddProduct_BarcodeRead(int Barcode)
        {
            gleProduct.EditValue = Barcode;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (ProductSelected != null)
                ProductSelected(ProductId, Artikel);

            if (includeSentez)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void gleProduct_EditValueChanged(object sender, EventArgs e)
        {
            //if (ProductId > 0)
            if (gleProduct.EditValue != null)
                btnAdd.Enabled = true;
        }

        private void AddProduct_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                btnAdd.PerformClick();
        }
    }
}